/*
 * mainwindow.cpp
 *
 *  Created on: 06.10.2014
 *      Author: Ralph
 */

#include "mainwindow.h"

#include <buildtime.h>

#include "listwidget.h"

#include <QAction>
#include <QDebug>
#include <QFileDialog>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QToolBar>

MainWindow::MainWindow( bool debug, bool resetSettings ) :
    m_debug( debug )
{
    setDockOptions( QMainWindow::AnimatedDocks |  QMainWindow::AllowNestedDocks | QMainWindow::AllowTabbedDocks | QMainWindow::VerticalTabs );

    createActions();
    createMenus();
    createToolBars();
    createDockWindows();
    createStatusBar();

    setWindowTitle( tr( "PaperBoy" ) );

    QIcon logo( ":/icons/eyes.png" );
    setWindowIcon( logo );

    setUnifiedTitleAndToolBarOnMac( false );

    if ( !resetSettings )
    {
        loadSettings();
    }
}

MainWindow::~MainWindow()
{
}

void MainWindow::closeEvent( QCloseEvent *event )
{
    saveSettings();
}

void MainWindow::loadSettings()
{
    QSettings settings;
    restoreGeometry( settings.value( "mainWindowGeometry" ).toByteArray() );
    restoreState( settings.value( "mainWindowState" ).toByteArray() );

    m_workDir = loadSetting( settings, "workDir" ).toString();

    QDir dir( m_workDir );
    if ( !dir.exists() || m_workDir == "" )
    {
        QFileDialog* fd = new QFileDialog( this, "Select work dir" );
        fd->setFileMode( QFileDialog::DirectoryOnly );
        fd->setOption( QFileDialog::ShowDirsOnly );
        fd->setAcceptMode( QFileDialog::AcceptOpen );

        QStringList fileNames;
        if ( fd->exec() )
        {
            fileNames = fd->selectedFiles();
        }
        if ( fileNames.size() == 1 )
        {
            m_workDir = fileNames[0];
        }

        delete fd;
    }
}

void MainWindow::saveSettings()
{
    QSettings settings;

    settings.setValue( "mainWindowGeometry", saveGeometry() );
    settings.setValue( "mainWindowState", saveState() );

    settings.setValue( "workDir", m_workDir );
}

QVariant MainWindow::loadSetting( QSettings &settings, QString key )
{
    if ( settings.contains( key ) )
    {
        return settings.value( key );
    }
    return QVariant();
}

void MainWindow::createActions()
{
    newAct = new QAction( QIcon( ":/icons/new.png" ), tr( "&New..." ), this );
    newAct->setShortcuts( QKeySequence::New );
    newAct->setStatusTip( tr( "New Dataset" ) );
    connect( newAct, SIGNAL( triggered() ), this, SLOT( slotNew() ) );

    openAct = new QAction( QIcon( ":/icons/open.png" ), tr( "&Open..." ), this );
    openAct->setShortcuts( QKeySequence::Open );
    openAct->setStatusTip( tr( "Load Dataset" ) );
    connect( openAct, SIGNAL( triggered() ), this, SLOT( slotOpen() ) );

    saveAct = new QAction( QIcon( ":/icons/save.png" ), tr( "&Save..." ), this );
    saveAct->setShortcuts( QKeySequence::Save );
    saveAct->setStatusTip( tr( "Save the current form letter" ) );
    connect( saveAct, SIGNAL( triggered() ), this, SLOT( slotSave() ) );

    quitAct = new QAction( tr( "&Quit" ), this );
    quitAct->setShortcuts( QKeySequence::Quit );
    quitAct->setStatusTip( tr( "Quit the application" ) );
    connect( quitAct, SIGNAL( triggered() ), this, SLOT( slotQuit() ) );

    aboutAct = new QAction( tr( "&About" ), this );
    aboutAct->setStatusTip( tr( "Show the application's About box" ) );
    connect( aboutAct, SIGNAL( triggered() ), this, SLOT( slotAbout() ) );

}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu( tr( "&File" ) );
    fileMenu->addAction( newAct );
    fileMenu->addAction( openAct );
    fileMenu->addAction( saveAct );
    separatorAct = fileMenu->addSeparator();
    fileMenu->addAction( quitAct );

    helpMenu = menuBar()->addMenu( tr( "&Help" ) );
    helpMenu->addAction( aboutAct );
}

void MainWindow::createToolBars()
{
    fileToolBar = addToolBar( tr( "File" ) );
    fileToolBar->addAction( newAct );
    fileToolBar->addAction( openAct );
    fileToolBar->addAction( saveAct );
    fileToolBar->setObjectName( "fileToolbar" );
}

void MainWindow::createStatusBar()
{

}

void MainWindow::createDockWindows()
{
    ListWidget* lw = new ListWidget( "paper list", this );
    addDockWidget( Qt::LeftDockWidgetArea, lw );
}

void MainWindow::slotNew()
{

}

void MainWindow::slotOpen()
{

}

void MainWindow::slotSave()
{

}

void MainWindow::slotAbout()
{
    QString hg = HGTIP;
    hg.remove( ";" );
    hg.remove( "changeset:" );
    hg.replace( " ", "" );

        QString message( "<b>PaperBoy development version " + hg +
    #ifdef __DEBUG__
                " (debug)"
    #elif __RELEASE__
                " (release)"
    #endif
                "<br><br> </b>"
                        "Index, summarize and categorize scientific papers.<br><br>"
                        "For questions please consult the <a href=\"https://code.google.com/p/paperboy/wiki/Main\"><b>documentation</b></a>.<br><br>"
                        "Please report bugs and feature requests <a href=\"https://code.google.com/p/paperboy/issues/list\"><b>here</b></a>.<br><br>" );

    QMessageBox::about( this, tr( "About PaperBoy" ), message );
}

void MainWindow::slotQuit()
{
    close();
}
